package com.txby.platformApi.component.web;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.Constant;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.platformCore.entity.SickDetailInfo;

/**
 * 患者详细信息
 */
@RestController
@RequestMapping("/pro/sickdetail")
public class SickDetailInfoController  extends CommonController {
	
	/**
	 * 所有用户列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("pro:sickdetail:info")
	public R list(@RequestParam Map<String, Object> params){
//		数据隔离
		String join="";
		String sql="SELECT t.*,t2.name deptName FROM  "+this.mBaseDao.tableName(SickDetailInfo.class)+" t,sys_dept t2 WHERE t.deptId=t2.dept_id" ;
		if(getUserId() != Constant.SUPER_ADMIN){
//			params.put("createUserId", getUserId());
			 join=getJoinSqlBracket();
			 sql+=" and t.deptId in "+getJoinSqlBracket();
		}
		String sickName=params.get("sickName")==null?"":params.get("sickName").toString();
		if(!ChkUtil.isEmptyAllObject(sickName)){
			if (sickName.contains("%") || sickName.contains("_")) {
				sickName = sickName.trim().replace("%", "\\%").replace("_", "\\_");
				sickName = "%" + sickName + "%";
				sql += " and t.sickName like #{pojo.sickName} escape '\\'";
				params.put("sickName", "%" + sickName.trim() + "%");
			} else {
				sickName = "%" + sickName.trim() + "%";
				sql += " and t.sickName like #{pojo.sickName} ";
				params.put("sickName", "%" + sickName.trim() + "%");
			}
		}
		sql += Query.getLastSortSql(params, " order by updateTime ");
		//查询列表数据
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), params);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}
	
 
	
	/**
	 * 查询信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("pro:sickdetail:info")
	public R info(@PathVariable("id") Long id){
		String sql="SELECT t.*,t2.name deptName FROM  "+this.mBaseDao.tableName(SickDetailInfo.class)+" t,sys_dept t2 WHERE t.deptId=t2.dept_id and t.id_='"+id+"'" ;
		Map sickdetail =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("sickdetail", sickdetail);
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@RequestMapping("/save")
	@RequiresPermissions("pro:sickdetail:save")
	public R save(@RequestBody SickDetailInfo sickdetail){
		ValidatorUtils.validateEntity(sickdetail, AddGroup.class);
		String sql="select * from "+this.mBaseDao.tableName(SickDetailInfo.class)+" where IDNO='"+sickdetail.getICNO()+"'";
		SickDetailInfo old=(SickDetailInfo) this.mBaseDao.findBySqlUniqueObject(sql, SickDetailInfo.class, null);
		 if(old!=null){
			 return R.ok().error("患者编号重复");
		 }
		 sickdetail.setCreater(getUserId().toString());
		 sickdetail.setUpdater(getUserId().toString());
			this.mBaseDao.saveObject(sickdetail);
		 
		return R.ok();
	}
	
	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@RequestMapping("/update")
	@RequiresPermissions("pro:sickdetail:update")
	public R update(@RequestBody  SickDetailInfo sickdetail){
		ValidatorUtils.validateEntity(sickdetail, AddGroup.class);
		   String sql="select * from "+this.mBaseDao.tableName(SickDetailInfo.class)+" where IDNO ="+sickdetail.getICNO()+" and id_!="+sickdetail.getId_()+" ";
		   SickDetailInfo old=(SickDetailInfo) this.mBaseDao.findBySqlUniqueObject(sql, SickDetailInfo.class, null);
		 if(old!=null){
			 return R.ok().error("患者编号重复");
		 }else{
			 old=(SickDetailInfo) this.mBaseDao.findBySqlUniqueObject("select * from "+this.mBaseDao.tableName(SickDetailInfo.class)+" where   id_="+sickdetail.getId_(), SickDetailInfo.class, null);
			 if(old!=null){
				 this.mBaseDao.updateObject(sickdetail);
			 }
		 }
		return R.ok();
	}
	
	
	
	
	
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@RequestMapping("/delete")
	@RequiresPermissions("pro:sickdetail:delete")
	public R delete(@RequestBody Long[] ids){
		 for(Long id:ids){
		    	this.mBaseDao.executeSql(" delete from "+this.mBaseDao.tableName(SickDetailInfo.class)+" where id_="+id+"", null);
		  }
			return R.ok();
	}
}
